<?php
//铃盘搜索记录

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

include_once('../lib/config.php');
include_once('../lib/Db.class.php');
include_once('../lib/Model.class.php');
include_once('../lib/Upyun.class.php');
include_once('../lib/function.php');

$redis = new Redis();  
$ret = $redis->connect("localhost","6379");  //php客户端设置的ip及端口
if(!$ret) die('redis connection failed');
$redis->auth(REDIS_PASS);
$redis->select(1);

$GLOBALS['config']['master']['db_database'] = 'mh_lnpan';
$GLOBALS['config']['slave']['db_database']  = 'mh_lnpan';

//$model = new Model('http://www.lnpan.com/index.php?m=api');
$model = new Db;

$lnpan_search_log_queue_key = 'LNPAN_SEARCH_LOG_QUEUE';
$limit = 5000;

while ($limit--) {
  $stop = $redis->get('LNPAN_QUEUE_STOP');
  if($stop) {
    die('stop');
  }

  echo $limit . PHP_EOL;

  $log = $redis->rPop($lnpan_search_log_queue_key);
  if($log) {
    list($time, $keyword, $count, $ip) = explode(',', $log);
    $keyword = trim($keyword);
    $keyword = str_replace("'", "", $keyword);
    $keyword = str_replace('"', '', $keyword);
    $keyword = str_replace('[', '', $keyword);
    $keyword = str_replace(']', '', $keyword);
    $keyword = str_replace('{', '', $keyword);
    $keyword = str_replace('}', '', $keyword);
    $keyword = str_replace(';', '', $keyword);
    $keyword = str_replace('@', '', $keyword);
    $keyword = str_replace('\\', '', $keyword);
    $keyword = str_replace("(", "", $keyword);
    $keyword = str_replace(")", "", $keyword);

    if( stristr($keyword, 'union') || stristr($keyword, 'select') || stristr($keyword, 'and') || 
        stristr($keyword, '??') || stristr($keyword, 'update') || stristr($keyword, 'delete') || 
        stristr($keyword, '../') || stristr($keyword, 'WEB-INF') || stristr($keyword, '.htm') ||
        stristr($keyword, '.xml') || stristr($keyword, '.php') || stristr($keyword, '.conf') ||
        stristr($keyword, '.ini') || stristr($keyword, '@print') || stristr($keyword, '$print') ||
        stristr($keyword, 'etc/') || stristr($keyword, '%25') ||
        stristr($keyword, 'create') || stristr($keyword, 'or') || stristr($keyword, 'limit') ) {
      continue;
    }

    if(mb_strlen($keyword, 'utf8') > 1) {
      $old = $model->find('ring_keyword', "keyword='$keyword'");
      if($old) {
        $update_date = array(
          'search_count' => $old['search_count']+1,
          'result_count' => $count ? $count : $old['result_count'],
          'last_time'    => strtotime($time) ? strtotime($time) : $old['last_time'],
          'last_ip'      => $ip ? $ip : $old['last_ip'],
        );
        $ret = $model->save('ring_keyword', "id='$old[id]'", $update_date);

      } else {
        $add_date = array(
          'keyword' => $keyword,
          'search_count' => 1,
          'result_count' => $count,
          'last_time'    => strtotime($time) ? strtotime($time) : time(),
          'last_ip'      => $ip,
        );
        $ret = $model->add('ring_keyword', $add_date);
      }

      if(!$ret) {
        print_r($model->error());
        //$redis->lPush($lnpan_search_log_queue_key, $log);
      } else {
        //var_dump($log);
      }
    }
    else {
      //var_dump($log);
    }
  }
  else {
    sleep(1);
    continue;
  }
}

$model->close();
